home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58606 / 58606.xpi / chrome / translator.jar / content / translator.js < prev    next >
Text File  |  2010-02-07  |  8KB  |  300 lines

  1.  
  2. (function(namespace, $)
  3. {
  4.     namespace.Translator = function(prefsManager, ui)
  5.     {
  6.         this.prefs = prefsManager;
  7.         this.ui = ui;
  8.     };
  9.     
  10.     namespace.Translator.STATUS_TRANSLATED = 1;
  11.     namespace.Translator.STATUS_NOT_DETECTED = 2;
  12.     namespace.Translator.STATUS_NOT_TRANSLATED = 3;
  13.     
  14.     namespace.Translator.STATE_DISABLED = 0;
  15.     namespace.Translator.STATE_ENABLED = 1;
  16.     namespace.Translator.STATE_ERROR = 2;
  17.     
  18.     namespace.Translator.prototype = {
  19.         initialized: false,
  20.         state: null,
  21.         ui: null,
  22.         prefs: null,
  23.         cache: {},
  24.         
  25.         init: function()
  26.         {
  27.             // set event listeners
  28.             $(document).bind('translatorPreferencesChanged.translator', this.preferencesChangedHandler.bind(this));
  29.             $(document).bind('translatorStateSwitched.translator', this.stateSwitchedHandler.bind(this));
  30.             $(document).bind('translatorLanguageChanged.translator', this.languageChangedHandler.bind(this));
  31.             $(document).bind('translatorShowContextMenuSwitched.translator', this.showContextMenuSwitchedHandler.bind(this));
  32.             $(document).bind('translatorShowToolbarSwitched.translator', this.showToolbarSwitchedHandler.bind(this));
  33.             $(document).bind('translatorShowStatusBarSwitched.translator', this.showStatusBarSwitchedHandler.bind(this));
  34.             $(document).bind('translatorTranslateSelection.translator', this.translateSelectionHandler.bind(this));
  35.             
  36.             this.initialized = true;
  37.             
  38.             // initialize ui
  39.             this.ui.init();
  40.             
  41.             // initial reload of translator
  42.             this.reload();
  43.         },
  44.         
  45.         reload: function()
  46.         {
  47.             // enable/disable translator
  48.             if(this.prefs.getPref('enabled')) {
  49.                 this.state = namespace.Translator.STATE_ENABLED;
  50.                 
  51.                 this.ui.showDefaultState();
  52.                 
  53.                 $(document).trigger('translatorEnabled.translator');
  54.             }
  55.             else {
  56.                 this.state = namespace.Translator.STATE_DISABLED;
  57.                 
  58.                 this.ui.showDisabledState();
  59.                 
  60.                 $(document).trigger('translatorDisabled.translator');
  61.             }
  62.             
  63.             // setup interface
  64.             this.setupUI();
  65.             
  66.             // translate by selection
  67.             if(this.prefs.getPref('translate.selection') && this.state == namespace.Translator.STATE_ENABLED) {
  68.                 $(document).trigger('translatorTranslateSelectionEnabled.translator');
  69.             }
  70.             else {
  71.                 $(document).trigger('translatorTranslateSelectionDisabled.translator');
  72.             }
  73.             
  74.             // translate by floating pane
  75.             if(this.prefs.getPref('translate.floating') && this.state == namespace.Translator.STATE_ENABLED) {
  76.                 $(document).trigger('translatorTranslateFloating.translator', true);
  77.             }
  78.             else {
  79.                 $(document).trigger('translatorTranslateFloating.translator', false);
  80.             }
  81.         },
  82.         
  83.         setupUI: function()
  84.         {
  85.             // load list of selected languages
  86.             this.ui.loadLanguages(this.getSelectedLanguages());
  87.             
  88.             // set language
  89.             var languageCode = this.prefs.getPref('language');
  90.             this.ui.setLanguage(languageCode, this.getLanguageName(languageCode));
  91.             
  92.             // set appearances
  93.             this.ui.showContextMenu(this.prefs.getPref('context.menu'));
  94.             this.ui.showToolbar(this.prefs.getPref('toolbar'));
  95.             this.ui.showStatusBar(this.prefs.getPref('statusbar'));
  96.             this.ui.showStatusBarIcon(this.prefs.getPref('status.icon'));
  97.             this.ui.showStatusBarLabel(this.prefs.getPref('status.label'));
  98.             
  99.             // set hotkey
  100.             this.ui.setHotKey(this.prefs.getPref('translate.hotkey.modifiers'), this.prefs.getPref('translate.hotkey.key'));
  101.             
  102.             // enable/disable translation by hotkey
  103.             if(this.prefs.getPref('translate.hotkey') && this.state == namespace.Translator.STATE_ENABLED) {
  104.                 this.ui.enableHotKey();
  105.             }
  106.             else {
  107.                 this.ui.disableHotKey();
  108.             }
  109.         },
  110.         
  111.         translate: function(text, callback)
  112.         {
  113.             if(!this.initialized || !this.isEnabled()) return;
  114.             
  115.             if(text.length == 0) return;
  116.             
  117.             if(text == this.cache.original) {
  118.                 callback(namespace.Translator.STATUS_TRANSLATED, this.cache.translation, this.cache.notice);
  119.                 
  120.                 return;
  121.             }
  122.             
  123.             // run detection and translation
  124.             this.detect(text, function(response) {
  125.                 if(!response || !response.language) {
  126.                     callback(namespace.Translator.STATUS_NOT_DETECTED, text);
  127.                     
  128.                     return;
  129.                 }
  130.                 
  131.                 this.translateRequest(text, response.language, this.prefs.getPref('language'), function(response, textWasLimited) {
  132.                     if(!response || !response.translatedText) {
  133.                         callback(namespace.Translator.STATUS_NOT_TRANSLATED, text);
  134.                         
  135.                         return;
  136.                     }
  137.                     
  138.                     var noticeText = '';
  139.                     var translatedText = response.translatedText;
  140.                     
  141.                     // add limited message (if needed)
  142.                     if(textWasLimited) {
  143.                         translatedText += '...';
  144.                         noticeText = $('#translator-properties').get(0).getString('truncatedTranslation');
  145.                     }
  146.                     
  147.                     // cache translation
  148.                     this.cache.original = text;
  149.                     this.cache.translation = translatedText;
  150.                     this.cache.notice = noticeText;
  151.                     
  152.                     // callback
  153.                     callback(namespace.Translator.STATUS_TRANSLATED, translatedText, noticeText);
  154.                 }.bind(this));
  155.             }.bind(this));
  156.         },
  157.         
  158.         detect: function(text, callback)
  159.         {
  160.             if(!this.initialized || !this.isEnabled()) return;
  161.             
  162.             if(text.length == 0) return;
  163.             
  164.             this.detectRequest(text, callback);
  165.         },
  166.         
  167.         detectRequest: function(text, callback)
  168.         {
  169.             if(text.length == 0) return;
  170.             
  171.             // request params
  172.             var params = {
  173.                 v: '1.0',
  174.                 q: text.substr(0, 500) // limit text to 500 characters since it's still enough to detect language
  175.             };
  176.             
  177.             // make request
  178.             $.ajax({
  179.                 url: 'http://ajax.googleapis.com/ajax/services/language/detect',
  180.                 data: params,
  181.                 dataType: 'json',
  182.                 success: function(data) {
  183.                     callback(data.responseData);
  184.                 }
  185.             });
  186.         },
  187.         
  188.         translateRequest: function(text, sourceLanguage, destLanguage, callback)
  189.         {
  190.             if(text.length == 0) return;
  191.             
  192.             var textWasLimited = false;
  193.             
  194.             // 5000 characters is the limit set by google
  195.             if(text.length > 5000) {
  196.                 text = text.substr(0, 5000);
  197.                 textWasLimited = true;
  198.             }
  199.             
  200.             // request params
  201.             var params = {
  202.                 v: '1.0',
  203.                 q: text,
  204.                 langpair: sourceLanguage + '|' + destLanguage
  205.             };
  206.             
  207.             // make request
  208.             $.ajax({
  209.                 url: 'http://ajax.googleapis.com/ajax/services/language/translate',
  210.                 type: 'POST',
  211.                 data: params,
  212.                 dataType: 'json',
  213.                 success: function(data) {
  214.                     callback(data.responseData, textWasLimited);
  215.                 }
  216.             });
  217.         },
  218.         
  219.         getLanguageName: function(code)
  220.         {
  221.             return namespace.Languages[code];
  222.         },
  223.         
  224.         getSelectedLanguages: function()
  225.         {
  226.             var selectedLanguages = {};
  227.             
  228.             var selectedLanguageCodes = this.prefs.getPref('languages.selected').split(',');
  229.             
  230.             $.each(selectedLanguageCodes, function(i, code) {
  231.                 if(namespace.Languages[code]) {
  232.                     selectedLanguages[code] = namespace.Languages[code];
  233.                 }
  234.             }.bind(this));
  235.             
  236.             return selectedLanguages;
  237.         },
  238.         
  239.         isEnabled: function()
  240.         {
  241.             return (this.state == namespace.Translator.STATE_ENABLED);
  242.         },
  243.         
  244.         isTranslationBySelectionEnabled: function()
  245.         {
  246.             return this.prefs.getPref('translate.selection');
  247.         },
  248.         
  249.         isTranslationByFloatingEnabled: function()
  250.         {
  251.             return this.prefs.getPref('translate.floating');
  252.         },
  253.         
  254.         
  255.         /* event handlers */
  256.         
  257.         translateSelectionHandler: function(e)
  258.         {
  259.             // pass event to current page
  260.             $(content.document).trigger(e);
  261.         },
  262.         
  263.         preferencesChangedHandler: function(e)
  264.         {
  265.             this.reload();
  266.         },
  267.         
  268.         stateSwitchedHandler: function(e)
  269.         {
  270.             var newState = !(this.state == namespace.Translator.STATE_ENABLED);
  271.             
  272.             this.prefs.setPref('enabled', newState);
  273.         },
  274.         
  275.         languageChangedHandler: function(e, languageCode)
  276.         {
  277.             this.prefs.setPref('language', languageCode);
  278.             
  279.             this.ui.setLanguage(languageCode, this.getLanguageName(languageCode));
  280.             
  281.             // clear cache from any previous translations
  282.             this.cache = {};
  283.         },
  284.         
  285.         showContextMenuSwitchedHandler: function(e, state)
  286.         {
  287.             this.prefs.setPref('context.menu', state);
  288.         },
  289.         
  290.         showToolbarSwitchedHandler: function(e, state)
  291.         {
  292.             this.prefs.setPref('toolbar', state);
  293.         },
  294.         
  295.         showStatusBarSwitchedHandler: function(e, state)
  296.         {
  297.             this.prefs.setPref('statusbar', state);
  298.         }
  299.     };
  300. })(com.igorgladkov.translator, translatorJQuery);